package com.fastdfs.service.impl;

import com.fastdfs.dao.AlbumImageitemsMapper;
import com.fastdfs.dao.ImageitemsMapper;
import com.fastdfs.pojo.AlbumImageitems;
import com.fastdfs.pojo.Imageitems;
import com.fastdfs.service.AlbumImageitemsService;
import com.fastdfs.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AlbumImageitemsServiceImpl implements AlbumImageitemsService {

    @Autowired
    private AlbumImageitemsMapper albumImageitemsMapper;
    @Autowired
    private ImageitemsMapper imageitemsMapper;

    @Override
    public void add(AlbumImageitems albumImageitems) throws Exception {
        albumImageitemsMapper.insertSelective( albumImageitems);

    }

    @Override
    public  List<Imageitems>  selectALL(AlbumImageitems albumImageitems) throws Exception {

        List<AlbumImageitems> select = albumImageitemsMapper.select(albumImageitems);

        List<Imageitems> list=new ArrayList<>();
        for (AlbumImageitems albumImageitem : select) {
            Imageitems imageitems = imageitemsMapper.selectByPrimaryKey(albumImageitem.getImageitemsId());
            list.add(imageitems);
        }
        // 判断图片 是否是 可访问的  如果图片不可访问那么 删除不可访问的路径
        for (int i = 0; i < list.size(); i++) {
            String image = list.get(i).getImage();
            boolean b = ImageUtils.existRource(image);
            if (!b){
                list.remove(i);
                System.out.println("访问不了"+image);
            }
        }

        return  list;
    }

    //删除整个相册  从后往前

    @Override
    public int delete(AlbumImageitems albumImageitems) {
        System.out.println(albumImageitems);
        try {
            //删除关联数据前先找到子相册图片内的id
            List<AlbumImageitems> select = albumImageitemsMapper.select(albumImageitems);
            System.out.println(select );
            for (AlbumImageitems imageitems : select) {
                //在删除子相册图片前先将物理 图片删除
                Imageitems imageitems1 = imageitemsMapper.selectByPrimaryKey(imageitems.getImageitemsId());
                System.out.println(imageitems1);
                Map<String,Object> mp=new HashMap();
                mp.put("group",imageitems1.getImageGroup());
                mp.put("fileName",imageitems1.getImagePath());
                // 删除物理图片
                String s = HttpUtils.doDelete("http://localhost:20210/album/deleteFile", mp);
                Result o = JsonTurnUtils.jsonTurnJavaBena(s, Result.class);
                System.out.println(o);
                if (!o.isFlag()){
                    throw new Exception("删除物理图片出了问题");
                }
                //删除子相册所有图片
                Imageitems imageitem=new Imageitems();
                imageitem.setId(imageitems.getImageitemsId());
                int delete1 =imageitemsMapper.deleteByPrimaryKey(imageitem);
            }
            System.out.println("子相册和物理图片 删除成功");
            //删除关联
            albumImageitemsMapper.delete(albumImageitems);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("删除失败");
            return 0;
        }
        return 1;
    }

    @Override
    public int deleteOne(AlbumImageitems albumImageitems) {
        return albumImageitemsMapper.delete(albumImageitems);
    }

    @Override
    public int count(AlbumImageitems albumImageitems) {
        return albumImageitemsMapper.selectCount(albumImageitems);
    }


}
